<template>
  <div class="login">
    <vue-particles
      class="login-bg"
      color="#39AFFD"
      :particleOpacity="0.7"
      :particlesNumber="100"
      shapeType="circle"
      :particleSize="4"
      linesColor="#8DD1FE"
      :linesWidth="1"
      :lineLinked="true"
      :lineOpacity="0.4"
      :linesDistance="150"
      :moveSpeed="3"
      :hoverEffect="true"
      hoverMode="grab"
      :clickEffect="true"
      clickMode="push"
    >
    </vue-particles>
    <!-- 登录面板 -->
    <div class="login-box">
      <div class="login-box-title">后台管理系统</div>
      <div class="login-box-from">
        <el-form
          :model="users"
          :rules="rules"
          ref="users"
          class="demo-ruleForm"
        >
          <el-form-item prop="username">
            <el-input
              v-model="users.username"
              placeholder="请输入用户名"
              size="medium"
            >
              <el-button slot="prepend" icon="el-icon-user"></el-button>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="users.password"
              placeholder="请输入密码"
              size="medium"
              type="password"
            >
              <el-button slot="prepend" icon="el-icon-key"></el-button>
            </el-input>
            <p
              style="
                color: red;
                font-size: 15px;
                margin-left: 4px;
                margin-bottom: 5px;
              "
            >
              {{ errormsg }}
            </p>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              size="medium"
              style="width: 100%"
              @click="submitForm('users')"
              >立即登陆</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { login } from "@/api/apis";
export default {
  data() {
    return {
      users: {
        // 登陆表单
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { message: "用户名不能为空", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { message: "密码不能为空", trigger: "blur" },
        ],
      },
      errormsg: "",
    };
  },
  mounted() {
    if (!sessionStorage.getItem("isRefresh")) {
      window.location.reload();
      sessionStorage.setItem("isRefresh", true);
    }
  },
  methods: {
    submitForm(users) {
      // console.log(this.users.username, this.users.password);
      // console.log(this.users);
      this.$refs[users].validate((valid) => {
        if (valid) {
          //如果通过验证 to do...
          login(this.users).then((res) => {
            // console.log(res);
            if (res.data.code == 200) {
              sessionStorage.setItem("username", res.data.data.username);
              sessionStorage.setItem("role", res.data.data.role);
              sessionStorage.setItem("nickname", res.data.data.nickname);
              this.$message({
                message: "恭喜你，登录成功",
                type: "success",
              });

              setInterval(() => {
                this.$router.push("/main/indextotal"); //改变hash地址
              }, 500);
            } else {
              this.errormsg = res.data.message;
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });

      // if (
      //   this.loginForm.username == "admin" &&
      //   this.loginForm.password == "admin"
      // ) {
      //   this.$router.push("/main/indextotal"); //改变hash地址
      // } else {
      //   this.errormsg = "用户名或者密码错误";
      // }
    },
  },
};
</script>

<style scoped>
.login {
  width: 100%;
  height: 100%;
  /* display: flex;
    justify-content: center;
    align-items: center;
    background-image: url('../../assets/image/login.jpg');
    background-repeat: no-repeat;
    background-size: 100% 100%;
    color: #cccccc; */
  position: relative;
}
.login-bg {
  width: 100%;
  height: 100%;
  background: #3e3e3e;
}
.login-box {
  width: 350px;
  /* height: 287px; */
  background: hsla(0, 0%, 100%, 0.3);
  border-radius: 5px;
  box-shadow: 0 0 2px #f7f7f7;
  border: 1px solid #f7f7f7;
  position: absolute;
  left: 50%;
  top: 50%;
  margin-left: -175px;
  margin-top: -150px;
}
.login-box-title {
  line-height: 50px;
  font-size: 20px;
  color: #ffffff;
  text-align: center;
  border-bottom: 1px solid #ffffff;
}
.login-box-from {
  width: 100%;
  height: auto;
  padding: 30px;
  box-sizing: border-box;
}
</style>